<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="名字" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="名字"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input
            v-model="queryParams.email"
            placeholder="邮箱"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="联系电话"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input
            v-model="queryParams.content"
            placeholder="内容"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon>
              <Search />
            </template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon>
              <Refresh />
            </template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column key="name" label="名字" prop="name" min-width="150" align="center" />
        <el-table-column key="email" label="邮箱地址" prop="email" min-width="150" align="center" />
        <el-table-column key="phone" label="联系电话" prop="phone" min-width="150" align="center" />
        <el-table-column
          key="content"
          label="留言内容"
          prop="content"
          min-width="150"
          align="center"
        />
        <el-table-column key="status" label="处理状态" prop="status" min-width="150" align="center">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'warning'">
              {{ scope.row.status === 1 ? "已处理" : "未处理" }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          key="replyTime"
          label="处理时间"
          prop="replyTime"
          min-width="150"
          align="center"
        />
        <el-table-column
          key="createTime"
          label="创建时间"
          prop="createTime"
          min-width="150"
          align="center"
        />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              v-hasPerm="['chat:mrt-message:edit']"
              type="primary"
              size="small"
              link
              @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon>
                <Edit />
              </template>
              处理
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 留言处理弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="600px"
      @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="名字" prop="name">
          <el-input v-model="formData.name" readonly />
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="formData.email" readonly />
        </el-form-item>

        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formData.phone" readonly />
        </el-form-item>

        <el-form-item label="留言内容" prop="content">
          <el-input v-model="formData.content" type="textarea" :rows="3" readonly />
        </el-form-item>

        <el-form-item label="处理状态" prop="status">
          <el-tag :type="formData.status === 1 ? 'success' : 'warning'">
            {{ formData.status === 1 ? "已处理" : "未处理" }}
          </el-tag>
        </el-form-item>

        <el-form-item label="创建时间" prop="createTime">
          <el-input v-model="formData.createTime" readonly />
        </el-form-item>

        <el-form-item label="处理内容" prop="reply">
          <el-input
            v-model="formData.reply"
            type="textarea"
            :rows="4"
            placeholder="请输入处理内容..."
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定处理</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import MrtMessageAPI from "@/api/mrt/mrt-message.js";

defineOptions({
  name: "MrtMessage",
  inheritAttrs: false,
});

const queryFormRef = ref();
const dataFormRef = ref();

const loading = ref(false);
const total = ref(0);

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
});

// 留言表格数据
const pageData = ref([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 留言表单数据
const formData = reactive({});

// 留言表单校验规则
const rules = reactive({
  reply: [{ required: true, message: "请输入处理内容", trigger: "blur" }],
});

/** 查询留言 */
function handleQuery() {
  loading.value = true;
  MrtMessageAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置留言查询 */
function handleResetQuery() {
  queryFormRef.value.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 打开留言处理弹窗 */
function handleOpenDialog(id) {
  dialog.visible = true;
  dialog.title = "处理留言";
  MrtMessageAPI.getFormData(id).then((data) => {
    Object.assign(formData, data);
  });
}

/** 提交处理表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;

      const submitData = {
        ...formData,
        status: 1, // 设置为已处理
      };

      MrtMessageAPI.update(formData.id, submitData)
        .then(() => {
          ElMessage.success("处理成功");
        })
        .finally(() => {
          loading.value = false;
          handleCloseDialog();
          handleResetQuery();
        });
    }
  });
}

/** 关闭留言弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  Object.keys(formData).forEach((key) => {
    formData[key] = undefined;
  });
}

onMounted(() => {
  handleQuery();
});
</script>
